package com.redsun.smallworld.ob.services;

import com.redsun.smallworld.ob.memory.KnightMemory;
import com.redsun.smallworld.ob.moudle.CityList;
import com.redsun.smallworld.ob.moudle.Knight;
import com.redsun.smallworld.ob.moudle.KnightList;
import com.redsun.smallworld.tools.JsonUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * Created by zhebinwang on 2017/7/20.
 */
public class MemoryService {

    //单例
    private MemoryService(){};
    private static MemoryService memoryService;

    public static MemoryService getintface(){
        if (memoryService==null){
            memoryService=new MemoryService();
        }
        return memoryService;
    }


    /**
     *  通过文件加在knight列表
     * **/
    public void doLoadKnight(){
        KnightList knightList=null;
        try {
            //通过一个函数读取文件
            knightList= JsonUtil.extractData(doReadFile("/Users/zhebinwang/dev/srces/RedSun/smallworld/target/cfg/knight.txt").toString(), KnightList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //KnightMemory.knightMap;

        for (int x=0;x<knightList.getKnightList().size();x++){
            KnightMemory.knightMap.put(knightList.getKnightList().get(x).getId(),knightList.getKnightList().get(x));
        }
    }
    /**
     *  通过文件加在city列表
     * **/
    public void doLoadCity(){
        CityList cityList=null;
        try {
            //通过一个函数读取文件
            cityList= JsonUtil.extractData(doReadFile("/Users/zhebinwang/dev/srces/RedSun/smallworld/target/cfg/city.txt").toString(), CityList.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //KnightMemory.knightMap;

        for (int x=0;x<cityList.getCityList().size();x++){
            KnightMemory.cityMap.put(cityList.getCityList().get(x).getId(),cityList.getCityList().get(x));
        }
    }
    /**
     * 打印knight map 里面的所有信息
     * 就是确认情况而已
     * */
    public void doLogShowKnightInfo() {

        //hash map 便利方式 第三种：推荐，尤其是容量大时
        System.out.println("通过Map.entrySet遍历key和value");
        for (Map.Entry<Integer, Knight> entry : KnightMemory.knightMap.entrySet()) {
            System.out.println("knight= " +entry.getValue().getName());
        }
    }

    //读取指定的文件

    private String doReadFile(String filename) throws  Exception{
        String pathName = filename;
        StringBuilder stringBuilder=new StringBuilder();
        File file=new File(pathName);
        if(file.isFile() && file.exists()) { //判断文件是否存在
            InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file), "GBK");//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                stringBuilder.append(lineTxt);
            }
            read.close();
        }
        System.out.println(stringBuilder.toString());
        return stringBuilder.toString();
    }


}
